package com.jt.controller;

import com.jt.pojo.User;
import org.omg.PortableInterceptor.USER_EXCEPTION;
import org.springframework.web.bind.annotation.*;

import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.List;

@RestController // 将类交给springMVC管理，返回值转为josn字符串
@CrossOrigin // 允许跨域
@RequestMapping("/axios")
public class AxiosController {
    /**
     * URL:http://localhost:8080/axios/getUserById?id=100
     *
     * @return user对象的JOSN
     */
    @RequestMapping("/getUserById")
    public User getUserById(Integer id) {
        User user = new User();
        user.setName("艾希");
        user.setAge(500);
        user.setSex("女");
        return user;
    }

    /**
     * URL:http://localhost:8080/axios/getUserByNA?id=100&name=xxx
     */
    @RequestMapping("/getUserByNA")
    public List<User> getUserByNA(User user) {
        List<User> list = new ArrayList<>();
        list.add(user);
        list.add(user);
        return list;
    }

    /**
     * restFul 方式
     * URL:http://localhost:8080/axios/findUserByNs/100/name
     */
    @RequestMapping("/findUserByNs/{age}/{name}")
    public List<User> findUserByNs(User user) {
        List<User> userList = new ArrayList<>();
        userList.add(user);
        userList.add(user);
        return userList;
    }

    /**
     * 用post方式请求
     * URL: "http://localhost:8080/axios/saveUser"
     * · 用 method 属性定义请求的类型
     */
    // @RequestMapping(value = "/saveUser",method = RequestMethod.POST)
    @PostMapping("/saveUser")// 与上诉功能一样 ，接受固定类型的请求
    public String saveUser(@RequestBody User user) {// @RequestBody 将JOSN对象转换为user对象（只有在post请求中使用）
        System.out.println(user);
        return "新增数据成功";
    }

    /**
     * URL : "http://localhost:8080/axios/updateUser"
     */
    @PutMapping("/updateUser")
    public String updateUser(@RequestBody User user) {
        System.out.println(user);
        return "修改成功";
    }
}
